//
//  SubjectViewController.m
//  LimitFree
//
//  Created by LZXuan on 15-6-25.
//  Copyright (c) 2015年 轩哥. All rights reserved.
//

#import "SubjectViewController.h"
#import "SubjectCell.h"
#import "SubjectModel.h"
#import "AppModel.h"
#import "DetailViewController.h"
@interface SubjectViewController ()

@end

@implementation SubjectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //修改父类之前的设置
    self.navigationItem.rightBarButtonItem=nil;
    self.navigationItem.leftBarButtonItem = nil;
    self.tableView.tableHeaderView = nil;
    //固定高
    self.tableView.rowHeight = 300;
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"SubjectCell" bundle:nil] forCellReuseIdentifier:@"SubjectCell"];
    //重写这两个方法
    [self firstDownload];
    [self creatRefreshView];
}
- (void)firstDownload{
    self.isRefreshing =NO;
    self.isLoadMore = NO;
    self.currentPage=1;
    NSString *url = [NSString stringWithFormat:kSubjectUrl,self.currentPage];
    //增加下载任务
    [self addTaskWithUrl:url isRefresh:NO];
}

- (void)addTaskWithUrl:(NSString *)url isRefresh:(BOOL)isRefresh{
    __weak typeof (self) weakSelf=self;
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"专题下载成功");
        NSArray *arr = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        for (NSDictionary *dic in arr) {
            SubjectModel *subjectModel = [[SubjectModel alloc] init];
            //KVC赋值
            [subjectModel setValuesForKeysWithDictionary:dic];
            //解析 application数组
            NSMutableArray *mary = [[NSMutableArray alloc] init];
            
            for (NSDictionary *obj in subjectModel.applications) {
                AppModel *model = [[AppModel alloc] init];
                [model setValuesForKeysWithDictionary:obj];
                [mary addObject:model];
            }
            //存放到subjectModel 的数组中
            subjectModel.applications = mary;
            
            [weakSelf.dataArr addObject:subjectModel];
            
        }
        //循环完了 刷新表格
        [weakSelf.tableView reloadData];
        //结束刷新
        [weakSelf endRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}
-(void)creatRefreshView{
    __weak typeof (self) weakSelf=self;
    //下拉刷新
    [self.tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (weakSelf.isRefreshing) {
            return ;
        }
        weakSelf.isRefreshing = YES;
        weakSelf.currentPage = 1;
        NSString *url = [NSString stringWithFormat:kSubjectUrl,weakSelf.currentPage];
        [weakSelf addTaskWithUrl:url isRefresh:YES];
    }];
    //上拉加载
    [self.tableView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (weakSelf.isLoadMore) {
            return ;
        }
        weakSelf.isLoadMore = YES;
        weakSelf.currentPage++;
        NSString *url = [NSString stringWithFormat:kSubjectUrl,weakSelf.currentPage];
        [weakSelf addTaskWithUrl:url isRefresh:YES];
    }];
}
//重写cell方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SubjectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SubjectCell" forIndexPath:indexPath];
    SubjectModel *model = self.dataArr[indexPath.row];
    //填充cell
    //要传入一个block  内部 要进行回调
    [cell showDataWithSubjectModel:model myBlock:^(NSString *appId){
        DetailViewController *detail = [[DetailViewController alloc] init];
        //传值
        detail.categoryType = self.categoryType;
        detail.applicationsId = appId;
        
    }];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}

@end
